package studio7i.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import studio7i.excepcion.DAOExcepcion;
import studio7i.modelo.Instrumento_Sala;
import studio7i.util.ConexionBD;

public class ArmaSalaDAO extends BaseDAO{
	public Instrumento_Sala armarSala(Instrumento_Sala isa) throws DAOExcepcion {
		
		//System.out.println("UsuarioDAO: insertar()");
		String query = "INSERT INTO INSTRUMENTO_SALA (idInstrumento,idSala,cantidad) VALUES (?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			//con.setAutoCommit(false);
			stmt = con.prepareStatement(query);
			
			stmt.setInt(1,isa.getInstrumento().getIdInstrumento());
			stmt.setInt(2,isa.getSala().getIdSala()); 
			stmt.setInt(3,isa.getCantidad());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
/*
			int idp = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				idp = rs.getInt(1);
			}
			usu.setIdUsuario(idp);
/*
			for (Respuesta ped : vo.getRespuestas()) {
				query = "INSERT INTO respuesta(texto, idpregunta) VALUES (?,?)";
				stmt = con.prepareStatement(query);
				
				stmt.setString(1, ped.getTexto());
				stmt.setInt(2, idp);
				
				int u = stmt.executeUpdate();
				if (u != 1) {
					throw new SQLException("No se pudo insertar");
				}
				int idd = 0;
				query = "SELECT LAST_INSERT_ID()";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				if (rs.next()) {
					idd = rs.getInt(1);
				}
				ped.setIdRespuesta(idd);
			}
			con.commit();			*/
		} catch (SQLException e) {
			/*try {
				con.rollback();
			} catch (SQLException e1) {
				throw new DAOExcepcion(e.getMessage());
			}*/
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			/*try {
				con.setAutoCommit(true);
			} catch (SQLException e) {
				throw new DAOExcepcion(e.getMessage());
			}*/
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return isa;
	}

}
